package cn.zlj.controller;

import cn.zlj.degr.degradationMethod;
import cn.zlj.domain.User;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {

    @Value("${test}")
    private String ts;

    @GetMapping("/{id}")
    @SentinelResource(value="getUser", blockHandler = "degradationMethod",blockHandlerClass = degradationMethod.class
    ,fallback = "fallbackMethod")
    public User getUser(@PathVariable("id") Long id){
        try {
            // 用于让服务慢响应
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new User(id,"宵宫","60出"+ts);
    }

    public User degradationMethod(@PathVariable("id") Long id, BlockException e){
        e.printStackTrace();
        return new User(-1L,"限流了","ggg");
    }
    public User fallbackMethod(@PathVariable("id") Long id, BlockException e){
        e.printStackTrace();
        return new User(-1L,"熔断了","ggg");
    }
}
